<template>
  <lay-container :fluid="true" style="padding: 10px;padding-top: 10px;">
    <lay-card>
      <lay-row>
        <lay-col :md="24">
          <lay-table :page="page" :columns="columns" :dataSource="dataSource" :default-toolbar="defaultToolbar"
            v-model:selectedKeys="selectedKeys" @change="change" :size="tablesize">
            <template v-slot:toolbar>
              <lay-button type="primary" size="sm" @click="add()" v-permission="PerKey.AppLink_AddOrEditView"
                prefix-icon="layui-icon-addition">新增类别
              </lay-button>
            </template>
            <template v-slot:ico="{ data }">
              <lay-icon :type="data.ico"></lay-icon>
            </template>
            <template v-slot:operator="{ data }">
              <lay-button-group>
                <lay-button type="primary" size="sm" @click="edit(data.id)" v-permission="PerKey.AppLink_AddOrEditView"
                  prefix-icon="layui-icon-edit">
                  编辑
                </lay-button>
                <lay-button type="danger" size="sm" @click="del(data)" v-permission="PerKey.AppLink_Delete"
                  prefix-icon="layui-icon-delete">删除
                </lay-button>
              </lay-button-group>
            </template>
          </lay-table>
        </lay-col>
      </lay-row>
    </lay-card>

    <lay-layer :title="addoredittip" v-model="modelVisible" :resize="true" :area="['400px']">
      <lay-card style="padding:10px;">
        <lay-row space="10">
          <lay-form :model="form">
            <lay-col :md="24">
              <lay-form-item label="类别名称" prop="cateName" required required-error-message="链接类别名称不能空">
                <lay-input v-model="form.cateName"></lay-input>
              </lay-form-item>
              <lay-col :md="24" class="saveBtn">
                <lay-button @click="submitClick" type="primary" v-permission="PerKey.AppLink_AddOrEdit"
                  prefix-icon="layui-icon-ok">
                  {{ addoredittip }}
                </lay-button>
              </lay-col>
            </lay-col>
          </lay-form>
        </lay-row>
      </lay-card>
    </lay-layer>
  </lay-container>
</template>
  
<script lang="ts" name="AppLinkCat" setup>
import { reactive, ref, toRefs } from "vue";
import { layer } from "@layui/layer-vue";
import { getCateList, delCateOne, getCateEdit, postCateAdd, putCateEdit } from "../../../../api/module/content/applink";
import { PerKey } from "../../../../library/permissionConst"
import { AppLinkCate } from '../../../../types/applink'
import { useRouter } from "vue-router"
const router = useRouter();
const selectedKeys = ref(["1"]);
const defaultToolbar = ref(true);
const page = ref({ total: 0, limit: 15, current: 1, showCount: true, showRefresh: true, limits: [10, 15, 20, 50, 100, 200] });
const tablesize = ref('md');
const columns = [
  { title: "Id", width: "50px", key: "id", align: "center", },
  { title: "名称", key: "cateName", align: "center", },
  { title: "操作", width: "200px", customSlot: "operator", key: "operator", align: "center", }
]
const dataSource = ref([])
//定义模型
const putData = reactive({
  queryModel: {
    pageNum: page.value.current,
    pageSize: page.value.limit,
  }
})
const { queryModel } = toRefs(putData)
//获取列表
const GetList = function () {
  getCateList(queryModel.value).then((res) => {
    if (res.status == 1) {
      dataSource.value = res.data.result
      page.value.total = res.data.totalNum
    }
  })
}
GetList();
const change = function ({ current, limit }: any) {
  queryModel.value.pageNum = current
  queryModel.value.pageSize = limit
  GetList();
};

const edit = function (id: number) {
  changeVisible(id)
}
const add = function () {
  changeVisible(0)
}

//添加或者编辑
const cids = ref({ targetList: [] as any, })
const modelVisible = ref(false)
const data = reactive({
  form: <AppLinkCate><unknown>[]
})
const { form } = toRefs(data)
let addoredittip = ref();
const changeVisible = function (id: number) {
  modelVisible.value = !modelVisible.value
  if (id == 0) {
    addoredittip.value = "新增类别";
    getCateEdit(id).then((res) => {
      cids.value = res.data.cateList
      form.value = {
        id: 0,
        cateName: "",
      }
    })
    return
  } else {
    addoredittip.value = "编辑类别";
    getCateEdit(id).then((res) => {
      cids.value = res.data.cateList
      if (res.status == 1) {
        let data = res.data.data
        form.value = data
      }
    })
    return
  }
}

const submitClick = function () {
  if (!form.value.cateName) {
    layer.msg("名称不能空")
    return
  }
  if (form.value.id == 0) {
    postCateAdd(form.value).then((res) => {
      done(res)
    });
  } else {
    putCateEdit(form.value).then((res) => {
      done(res)
    });
  }
};
const done = (res: any) => {
  if (res.status == 1) {
    layer.msg(res.msg, () => { });
    modelVisible.value = !modelVisible.value
    GetList()
  } else {
    layer.msg(res.msg, { icon: 2 });
    return false;
  }
}

//删除
const del = function (data: any) {
  layer.confirm("确认删除ID: " + data.id + " " + data.cateName + "  链接类别吗？", {
    title: "警告", move: "true", shade: "false", shadeClose: "true",
    btn: [{
      text: '确认', callback: (layerId: number) => {
        delCateOne(data.id).then((res) => {
          if (res.status == 1) {
            layer.msg(res.msg, { time: 1000, icon: 1 }, () => {
              GetList()
            });
          } else {
            layer.msg(res.msg);
          }
          layer.close(layerId);
        })
      }
    }, {
      text: '取消', callback: function (layerId: number) {
        layer.close(layerId);
      }
    }]
  })
}
</script>
<style scoped>

</style>